import { mapState, mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters(['storeCompanyId']),
    ...mapState({
      userRole: state => state.userInfo.rname,
      companyIdByUser: state => state.userInfo.companyId
    })
  },
  data: () => ({
    companyId: ''
  }),
  methods: {
    hasPremission() {
      const vm = this
      const userRole = vm.userRole
      const storeCompanyId = vm.storeCompanyId // 非user绑定的companyId
      const companyIdByUser = vm.companyIdByUser // userInfo绑定的companyId

      /* storeCompanyId 并且用户为运营账号，代理商，管理员，演示账号 */

      if (
        userRole === 'ROLE_OPERATION' ||
        userRole === 'ROLE_ADMIN' ||
        userRole === 'ROLE_DEMO'
      ) {
        if (storeCompanyId) {
          vm.companyId = storeCompanyId
        } else {
          console.info(`当前用户角色${userRole} storeCompanyId 未获取`)
        }
      } else if (userRole === 'ROLE_USER') {
        /** companyId 角色为用户角色*/
        if (companyIdByUser) {
          this.companyId = companyIdByUser
        } else {
          console.info(`当前用户角色${userRole} companyIdByUser 未获取`)
        }
      } else if (userRole === 'ROLE_PLATFORM') {
        /** storeCompanyId 角色为 ROLE_PLATFORM*/
        if (storeCompanyId) {
          vm.companyId = storeCompanyId
        } else {
          console.info(`当前用户角色${userRole}企业id未获取`)
        }
      }
    }
  },
  mounted() {
    this.hasPremission()
  }
}
